﻿/*--------------------------------------*/
//SpecialCardModel->ChiListMode->ChiCardItemModel
//
//
//

using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpecialCardModel : MonoBehaviour {

    public GameObject ChiListShow;
    public GameObject Select1Btn;
    public GameObject Select2Btn;
    public GameObject Select3Btn;
    public GameObject SelectGuoBtn;
    public ChiListMode chiListModel;
    public MAHJONG_ACTION_TYPE Select1BtnStatu;
    public MAHJONG_ACTION_TYPE Select2BtnStatu;
    public MAHJONG_ACTION_TYPE Select3BtnStatu;
    EginMahjong mahjong = EginMahjong.Instance;

    public static int SelectIndex;// 选择吃牌所在的索引值
    public int tempIncard { get; set; }
    public int huCard;
    private void Awake()
    {
        UIEventListener.Get(Select1Btn).onClick = ButtonClick;
        UIEventListener.Get(Select2Btn).onClick = ButtonClick;
        UIEventListener.Get(Select3Btn).onClick = ButtonClick;
        UIEventListener.Get(SelectGuoBtn).onClick = ButtonClick;
        
    }
    private void Start()
    {

    }
    void ButtonClick(GameObject button)
    {
        mahjong.mahjongSpecialCardItem.Clear();
        mahjong.havaSpecailCard = false;
        if (button.name == "Select1Btn")
        {
            if (button.GetComponent<UISprite>().spriteName == "hu")
            {
                MahjongPlayRequest MR = new Pb.MahjongPlayRequest { ActionType = Select1BtnStatu, InCard = huCard };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(MR, MsgId.MahjongPlayRequest);
            }
            else {
                MahjongPlayRequest MR = new Pb.MahjongPlayRequest { ActionType = Select1BtnStatu, InCard = tempIncard };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(MR, MsgId.MahjongPlayRequest);
            }
            Destroy(this.gameObject);
        }
        else if (button.name == "Select2Btn")
        {

            if (button.GetComponent<UISprite>().spriteName == "hu")
            {
                MahjongPlayRequest MR = new Pb.MahjongPlayRequest { ActionType = Select2BtnStatu, InCard = huCard };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(MR, MsgId.MahjongPlayRequest);
            }
            else
            {
                MahjongPlayRequest MR = new Pb.MahjongPlayRequest { ActionType = Select2BtnStatu, InCard = tempIncard };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(MR, MsgId.MahjongPlayRequest);
            }
            
            Destroy(this.gameObject);
        }
        else if (button.name == "Select3Btn")
        {
            if (button.GetComponent<UISprite>().spriteName == "hu")
            {
                MahjongPlayRequest MR = new Pb.MahjongPlayRequest { ActionType = Select3BtnStatu, InCard = huCard };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(MR, MsgId.MahjongPlayRequest);
            }
            else
            {
                MahjongPlayRequest MR = new Pb.MahjongPlayRequest { ActionType = Select3BtnStatu, InCard = tempIncard };
                SocketManager socketManager = SocketManager.Instance;
                socketManager.SendProtobuf1(MR, MsgId.MahjongPlayRequest);
            }
            
            Destroy(this.gameObject);
        }
        else if (button.name == "SelectGuoBtn")
        {
            SendPassMessage();
            Destroy(this.gameObject);
        }
        
    }


   
    public void LoadSpecialCard() {

        int tempIntChi=0;
        int tempIntAddGang = 0;
        int tempIntLightGang = 0;
        int tempIntDarkGang = 0;
        int tempIntPeng = 0;
        int tempIntSelfhu = 0;
        int tempIntNormalhu = 0;
        int tempIntGanghu = 0;
        foreach (MahjongSpecialCardItem i in mahjong.mahjongSpecialCardItem)
        {
            if (i.ActionType == MAHJONG_ACTION_TYPE.Chi)
            {
                tempIntChi++;
                tempIncard = i.TargetCard;
                SendPassMessage();
                Destroy(this.gameObject);
            }
            else if (i.ActionType == MAHJONG_ACTION_TYPE.Peng)
            {
                tempIntPeng++;
                tempIncard = i.TargetCard;
            }
            else if (i.ActionType == MAHJONG_ACTION_TYPE.AddGang)
            {
                tempIntAddGang++;
                tempIncard = i.TargetCard;

            }
            else if (i.ActionType == MAHJONG_ACTION_TYPE.DarkGang)
            {
                tempIntDarkGang++;
                tempIncard = i.TargetCard;

            }
            else if (i.ActionType == MAHJONG_ACTION_TYPE.LightGang)
            {
                tempIntLightGang++;
                tempIncard = i.TargetCard;

            }
            else if (i.ActionType == MAHJONG_ACTION_TYPE.SelfHu)
            {
                tempIntSelfhu++;
                huCard = i.TargetCard;

            }
            else if (i.ActionType == MAHJONG_ACTION_TYPE.GangHu)
            {
                tempIntGanghu++;
                huCard = i.TargetCard;

            }
            else if (i.ActionType == MAHJONG_ACTION_TYPE.NormalHu)
            {
                tempIntNormalhu++;
                huCard = i.TargetCard;

            }
        }
        Debug.Log("碰的数量：" + tempIntPeng+"胡的数量："+ tempIntSelfhu + ","+ tempIntGanghu + ","+ tempIntNormalhu);
       
        if (mahjong.mahjongSpecialCardItem.Count == 1)
        { 
            Debug.Log("一个特殊牌");
            #region one
            if (tempIntPeng == 1)
            {
                Select1Btn.SetActive(true);
                Select1Btn.GetComponent<UISprite>().spriteName = "peng";
                Select1BtnStatu = MAHJONG_ACTION_TYPE.Peng;
            }
            else if (tempIntAddGang == 1)
            {
                Select1Btn.SetActive(true);
                Select1Btn.GetComponent<UISprite>().spriteName = "gang";
                Select1BtnStatu = MAHJONG_ACTION_TYPE.AddGang;

            }
            else if (tempIntLightGang == 1)
            {
                Select1Btn.SetActive(true);
                Select1Btn.GetComponent<UISprite>().spriteName = "gang";
                Select1BtnStatu = MAHJONG_ACTION_TYPE.LightGang;

            }
            else if (tempIntDarkGang == 1)
            {
                Select1Btn.SetActive(true);
                Select1Btn.GetComponent<UISprite>().spriteName = "gang";
                Select1BtnStatu = MAHJONG_ACTION_TYPE.DarkGang;

            }
            else if (tempIntSelfhu == 1)
            {

                Select1Btn.SetActive(true);
                Select1Btn.GetComponent<UISprite>().spriteName = "hu";
                Select1BtnStatu = MAHJONG_ACTION_TYPE.SelfHu;

            }
            else if (tempIntNormalhu == 1)
            {
                Select1Btn.SetActive(true);
                Select1Btn.GetComponent<UISprite>().spriteName = "hu";
                Select1BtnStatu = MAHJONG_ACTION_TYPE.NormalHu;

            }
            else if (tempIntGanghu == 1)
            {
                Select1Btn.SetActive(true);
                Select1Btn.GetComponent<UISprite>().spriteName = "hu";
                Select1BtnStatu = MAHJONG_ACTION_TYPE.GangHu;
            }
            #endregion
        }
        else if (mahjong.mahjongSpecialCardItem.Count == 2)
        {
            Debug.Log("2个特殊牌");
            #region  two

            Debug.Log("碰的数量：" + tempIntPeng + "胡的数量：" + tempIntSelfhu + "," + tempIntGanghu + "," + tempIntNormalhu);
            if (tempIntPeng == 1)
            {
                if (tempIntPeng == 1)
                {
                    Select1Btn.SetActive(true);
                    Select1Btn.GetComponent<UISprite>().spriteName = "peng";
                    Select1BtnStatu = MAHJONG_ACTION_TYPE.Peng;
                }
                if (tempIntAddGang == 1)
                {
                    Select2Btn.SetActive(true);
                    Select2Btn.GetComponent<UISprite>().spriteName = "gang";
                    Select2BtnStatu = MAHJONG_ACTION_TYPE.AddGang;
                }
                if (tempIntLightGang == 1)
                {
                    Select2Btn.SetActive(true);
                    Select2Btn.GetComponent<UISprite>().spriteName = "gang";
                    Select2BtnStatu = MAHJONG_ACTION_TYPE.LightGang;
                }
                if (tempIntDarkGang == 1)
                {
                    Select2Btn.SetActive(true);
                    Select2Btn.GetComponent<UISprite>().spriteName = "gang";
                    Select2BtnStatu = MAHJONG_ACTION_TYPE.DarkGang;
                }
                if (tempIntSelfhu == 1)
                {
                    Debug.Log("胡自己");
                    Select2Btn.SetActive(true);
                    Select2Btn.GetComponent<UISprite>().spriteName = "hu";
                    Select2BtnStatu = MAHJONG_ACTION_TYPE.SelfHu;
                }
                if (tempIntNormalhu == 1)
                {
                    Debug.Log("正常胡");
                    Select2Btn.SetActive(true);
                    Select2Btn.GetComponent<UISprite>().spriteName = "hu";
                    Select2BtnStatu = MAHJONG_ACTION_TYPE.NormalHu;
                }
                if (tempIntGanghu == 1)
                {
                    Debug.Log("杠胡");
                    Select2Btn.SetActive(true);
                    Select2Btn.GetComponent<UISprite>().spriteName = "hu";
                    Select2BtnStatu = MAHJONG_ACTION_TYPE.GangHu;
                }
            }
            else
            {
                if (tempIntAddGang == 1)
                {
                    Select1Btn.SetActive(true);
                    Select1Btn.GetComponent<UISprite>().spriteName = "gang";
                    Select1BtnStatu = MAHJONG_ACTION_TYPE.AddGang;
                }
                if (tempIntLightGang == 1)
                {
                    Select1Btn.SetActive(true);
                    Select1Btn.GetComponent<UISprite>().spriteName = "gang";
                    Select1BtnStatu = MAHJONG_ACTION_TYPE.LightGang;
                }
                if (tempIntDarkGang == 1)
                {
                    Select1Btn.SetActive(true);
                    Select1Btn.GetComponent<UISprite>().spriteName = "gang";
                    Select1BtnStatu = MAHJONG_ACTION_TYPE.DarkGang;
                }
                if (tempIntSelfhu == 1)
                {
                    Debug.Log("胡自己");
                    Select2Btn.SetActive(true);
                    Select2Btn.GetComponent<UISprite>().spriteName = "hu";
                    Select2BtnStatu = MAHJONG_ACTION_TYPE.SelfHu;
                }
                if (tempIntNormalhu == 1)
                {
                    Debug.Log("正常胡");
                    Select2Btn.SetActive(true);
                    Select2Btn.GetComponent<UISprite>().spriteName = "hu";
                    Select2BtnStatu = MAHJONG_ACTION_TYPE.NormalHu;
                }
                if (tempIntGanghu == 1)
                {
                    Debug.Log("杠胡");
                    Select2Btn.SetActive(true);
                    Select2Btn.GetComponent<UISprite>().spriteName = "hu";
                    Select2BtnStatu = MAHJONG_ACTION_TYPE.GangHu;
                }
            }

            

            #endregion
        }
        else if(mahjong.mahjongSpecialCardItem.Count==3)
        { 
            Debug.Log("3个特殊牌");
            #region  three
            Debug.Log("碰的数量：" + tempIntPeng + "胡的数量：" + tempIntSelfhu + "," + tempIntGanghu + "," + tempIntNormalhu);
            if (tempIntPeng == 1)
            {
                Select1Btn.SetActive(true);
                Select1Btn.GetComponent<UISprite>().spriteName = "peng";
                Select1BtnStatu = MAHJONG_ACTION_TYPE.Peng;
            }
            if (tempIntAddGang == 1)
            {
                Select2Btn.SetActive(true);
                Select2Btn.GetComponent<UISprite>().spriteName = "gang";
                Select2BtnStatu = MAHJONG_ACTION_TYPE.AddGang;
            }
            if (tempIntLightGang == 1)
            {
                Select2Btn.SetActive(true);
                Select2Btn.GetComponent<UISprite>().spriteName = "gang";
                Select2BtnStatu = MAHJONG_ACTION_TYPE.LightGang;
            }
            if (tempIntDarkGang == 1)
            {
                Select2Btn.SetActive(true);
                Select2Btn.GetComponent<UISprite>().spriteName = "gang";
                Select2BtnStatu = MAHJONG_ACTION_TYPE.DarkGang;
            }
            if (tempIntSelfhu == 1)
            {
                Debug.Log("胡自己");
                Select3Btn.SetActive(true);
                Select3Btn.GetComponent<UISprite>().spriteName = "hu";
                Select3BtnStatu = MAHJONG_ACTION_TYPE.SelfHu;
            }
            if (tempIntNormalhu == 1)
            {
                Debug.Log("正常胡");
                Select3Btn.SetActive(true);
                Select3Btn.GetComponent<UISprite>().spriteName = "hu";
                Select3BtnStatu = MAHJONG_ACTION_TYPE.NormalHu;
            }
            if (tempIntGanghu == 1)
            {
                Debug.Log("杠胡");
                Select3Btn.SetActive(true);
                Select3Btn.GetComponent<UISprite>().spriteName = "hu";
                Select3BtnStatu = MAHJONG_ACTION_TYPE.GangHu;
            }
            #endregion
        }


    }
   
    public void LoadChiCardList(List<MahjongSpecialCardItem> tempList, int tempIntChi) {
        ChiListShow.SetActive(true);
        chiListModel.LoadCardList(tempList, tempIntChi);
    }
    public void LoadChiCard(List<MahjongSpecialCardItem> tempList)
    {
        
    }
    void SendPassMessage()
    {
        MahjongPlayRequest MR = new Pb.MahjongPlayRequest { ActionType = MAHJONG_ACTION_TYPE.MjPass};
        SocketManager socketManager = SocketManager.Instance;
        socketManager.SendProtobuf1(MR, MsgId.MahjongPlayRequest);
    }

}
